home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 16 windows forms / windowsformsdemo / dyncontrolform.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  5.4 KB  |  140 lines

  1. Public Class DynControlForm
  2.     Inherits System.Windows.Forms.Form
  3.  
  4. #Region " Windows Form Designer generated code "
  5.  
  6.     Public Sub New()
  7.         MyBase.New()
  8.  
  9.         'This call is required by the Windows Form Designer.
  10.         InitializeComponent()
  11.  
  12.         'Add any initialization after the InitializeComponent() call
  13.  
  14.     End Sub
  15.  
  16.     'Form overrides dispose to clean up the component list.
  17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  18.         If disposing Then
  19.             If Not (components Is Nothing) Then
  20.                 components.Dispose()
  21.             End If
  22.         End If
  23.         MyBase.Dispose(disposing)
  24.     End Sub
  25.     Friend WithEvents lblStatus As System.Windows.Forms.Label
  26.     Friend WithEvents chkUpperCase As System.Windows.Forms.CheckBox
  27.     Friend WithEvents btnCreateControls As System.Windows.Forms.Button
  28.  
  29.     'Required by the Windows Form Designer
  30.     Private components As System.ComponentModel.Container
  31.  
  32.     'NOTE: The following procedure is required by the Windows Form Designer
  33.     'It can be modified using the Windows Form Designer.  
  34.     'Do not modify it using the code editor.
  35.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  36.         Me.btnCreateControls = New System.Windows.Forms.Button()
  37.         Me.chkUpperCase = New System.Windows.Forms.CheckBox()
  38.         Me.lblStatus = New System.Windows.Forms.Label()
  39.         Me.SuspendLayout()
  40.         '
  41.         'btnCreateControls
  42.         '
  43.         Me.btnCreateControls.Location = New System.Drawing.Point(16, 24)
  44.         Me.btnCreateControls.Name = "btnCreateControls"
  45.         Me.btnCreateControls.Size = New System.Drawing.Size(136, 32)
  46.         Me.btnCreateControls.TabIndex = 0
  47.         Me.btnCreateControls.Text = "Create Controls"
  48.         '
  49.         'chkUpperCase
  50.         '
  51.         Me.chkUpperCase.Location = New System.Drawing.Point(168, 64)
  52.         Me.chkUpperCase.Name = "chkUpperCase"
  53.         Me.chkUpperCase.Size = New System.Drawing.Size(240, 16)
  54.         Me.chkUpperCase.TabIndex = 2
  55.         Me.chkUpperCase.Text = "Convert keys to upper case"
  56.         '
  57.         'lblStatus
  58.         '
  59.         Me.lblStatus.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
  60.         Me.lblStatus.Location = New System.Drawing.Point(168, 24)
  61.         Me.lblStatus.Name = "lblStatus"
  62.         Me.lblStatus.Size = New System.Drawing.Size(336, 24)
  63.         Me.lblStatus.TabIndex = 1
  64.         '
  65.         'DynControlForm
  66.         '
  67.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
  68.         Me.ClientSize = New System.Drawing.Size(568, 269)
  69.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.chkUpperCase, Me.lblStatus, Me.btnCreateControls})
  70.         Me.Name = "DynControlForm"
  71.         Me.Text = "DynControlForm"
  72.         Me.ResumeLayout(False)
  73.  
  74.     End Sub
  75.  
  76. #End Region
  77.  
  78.     ' create any number of controls dynamically
  79.  
  80.     Private Sub btnCreateControls_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateControls.Click
  81.         Dim answer As String
  82.         Dim index As Integer
  83.  
  84.         answer = InputBox("How many controls?", "Dynamic Control Creation", "5")
  85.         If answer = "" Then Exit Sub
  86.  
  87.         For index = 1 To CInt(answer)
  88.             ' Create the textbox
  89.             Dim tb As New TextBox()
  90.             tb.Size = New System.Drawing.Size(400, 30)
  91.             tb.Location = New System.Drawing.Point(50, 40 + index * 40)
  92.             'tb.Visible = True
  93.             ' we need this to identify the control
  94.             tb.Name = "TextBox #" & CStr(index)
  95.             ' add to the controls collection
  96.             Me.Controls.Add(tb)
  97.  
  98.             ' prepare event handlers
  99.             AddHandler tb.KeyPress, AddressOf TextBox_KeyPress
  100.             AddHandler tb.TextChanged, AddressOf TextBox_TextChanged
  101.             AddHandler tb.MouseEnter, AddressOf TextBox_MouseEnter
  102.             AddHandler tb.MouseLeave, AddressOf TextBox_MouseLeave
  103.         Next
  104.     End Sub
  105.  
  106.     ' a generic control handler that caps the pressed key
  107.  
  108.     Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
  109.         ' exit if no conversion to upper case is required
  110.         If Not chkUpperCase.Checked Then Exit Sub
  111.         ' exit if char doesn't have to be converted
  112.         If Not Char.IsLower(e.KeyChar) Then Exit Sub
  113.  
  114.         ' insert the key there
  115.         DirectCast(sender, TextBox).SelectedText = Char.ToUpper(e.KeyChar)
  116.         ' signal that this key has been dealt with
  117.         e.Handled = True
  118.     End Sub
  119.  
  120.     ' generic MouseEnter handler that changes the background color
  121.  
  122.     Private Sub TextBox_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs)
  123.         lblStatus.Text = "MouseEnter in " & CStr(CType(sender, TextBox).Name)
  124.         DirectCast(sender, TextBox).BackColor = Color.Yellow
  125.     End Sub
  126.  
  127.     ' generic MouseLeave handler that restores the background color
  128.  
  129.     Private Sub TextBox_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs)
  130.         lblStatus.Text = "MouseLeave in " & CStr(CType(sender, TextBox).Name)
  131.         DirectCast(sender, TextBox).BackColor = SystemColors.Window
  132.     End Sub
  133.  
  134.     ' generic TextChanged event handler
  135.  
  136.     Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
  137.         lblStatus.Text = "TextChanged in " & CStr(CType(sender, TextBox).Name)
  138.     End Sub
  139. End Class
  140.